﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
using System.Data;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using eTMS.Controllers;
using eTMS.Controllers.CaHocService;
using eTMS.Controllers.ThuTrongTuanService;
using eTMS.Controllers.TietHocService;

namespace eTMS.UserControls
{
    public partial class ChonThoiGian : UserControl
    {
        public ChonThoiGian()
        {
            InitializeComponent();
        }
        public const int STARTCOL = 4;
        public DataTable TableTime { get; set; }
        public List<ThuTrongTuanEntity> ListThuTrongTuan { set; get; }
        public List<CaHocEntity> ListCaHoc { set; get; }
        public List<TietHocEntity> ListTietHoc { set; get; }
        public List<TietHocEntity> GetListCheckedTietHoc()
        {
            List<TietHocEntity> ls = new List<TietHocEntity>();
            foreach (DataRow row in TableTime.Rows)
            {
                for (int i = STARTCOL; i < TableTime.Columns.Count; i++)
                {

                    if (Convert.ToBoolean(row[i]))
                    {
                        ls.Add(ListTietHoc.Find(delegate(TietHocEntity oTiet) { return row["TietHoc_ID"].ToString().Equals(oTiet.TietHoc_ID.ToString()); }));
                        break;
                    }
                }
            }
            return ls;
        }
        public List<ThuTrongTuanEntity> GetListCheckedThuTrongTuan()
        {
            List<ThuTrongTuanEntity> ls = new List<ThuTrongTuanEntity>();

            for (int i = STARTCOL; i < TableTime.Columns.Count; i++)
            {
                foreach (DataRow row in TableTime.Rows)
                {
                    if (Convert.ToBoolean(row[i]))
                    {
                        ls.Add(ListThuTrongTuan[i - STARTCOL]);
                        break;
                    }
                }
            }
            return ls;
        }
        private bool IsLoading = false;
        public void LoadThoiGian()
        {
            IsLoading=true;
            TableTime = new DataTable();
            DataColumn col = TableTime.Columns.Add("Ca");
            col.Caption = "Ca học";
            col = TableTime.Columns.Add("Tiet");
            col.Caption = "Tiết học";
            col = TableTime.Columns.Add("TietHoc_ID");
            col = TableTime.Columns.Add("CaHoc_ID");

            foreach (ThuTrongTuanEntity thuTrongTuanEntity in ListThuTrongTuan)
            {
                col = TableTime.Columns.Add("col" + thuTrongTuanEntity.ThuTrongTuan_ID.ToString(), typeof(bool));
                col.Caption = thuTrongTuanEntity.TenThu;
            }
            foreach (CaHocEntity caHocEntity in ListCaHoc)
            {
                foreach (TietHocEntity tietHocEntity in ListTietHoc.FindAll(delegate(TietHocEntity oFind) { return oFind.CaHoc_ID.Equals(caHocEntity.CaHoc_ID); }))
                {
                    DataRow row = TableTime.NewRow();
                    row["Ca"] = caHocEntity.TenCa;
                    row["CaHoc_ID"] = caHocEntity.CaHoc_ID;
                    row["Tiet"] = tietHocEntity.TenTietHoc;
                    row["TietHoc_ID"] = tietHocEntity.TietHoc_ID;
                    TableTime.Rows.Add(row);
                }
            }

            gridThoiGian.AutoGenerateColumns = true;
            gridThoiGian.DataSource = TableTime;
            gridThoiGian.Columns["Ca"].HeaderText = "Ca";
            gridThoiGian.Columns["Tiet"].HeaderText = "Tiết";
            gridThoiGian.Columns["Ca"].ReadOnly = true;
            gridThoiGian.Columns["Tiet"].ReadOnly = true;
            gridThoiGian.Columns["TietHoc_ID"].Visible = false;
            gridThoiGian.Columns["CaHoc_ID"].Visible = false;
            foreach (ThuTrongTuanEntity thuTrongTuanEntity in ListThuTrongTuan)
            {
                gridThoiGian.Columns["col" + thuTrongTuanEntity.ThuTrongTuan_ID.ToString()].HeaderText = thuTrongTuanEntity.TenThu;
                gridThoiGian.Columns["col" + thuTrongTuanEntity.ThuTrongTuan_ID.ToString()].Tag = thuTrongTuanEntity.ThuTrongTuan_ID;
            }
            gridThoiGian.AllowMergeColumn(0);
            gridThoiGian.DoMerge();
            gridThoiGian.Refresh();
            //List<string> ls = LichHocLopTinChiController.GetListConfig("TIME_OFCOURSE_INWEEK");
            //if (ls.Count == 0)
            //{
            CheckAllTime(true, 0);
            //}
            //else
            //{
            //    foreach (DataRow r in TableTime.Rows)
            //    {
            //        foreach (ThuTrongTuanEntity thuTrongTuanEntity in ListThuTrongTuan)
            //        {
            //            r["col" + thuTrongTuanEntity.ThuTrongTuan_ID.ToString()] = ls.Contains(thuTrongTuanEntity.ThuTrongTuan_ID.ToString() + "_" + r["TietHoc_ID"].ToString());
            //        }
            //    }
            //}
            gridThoiGian.AutoResizeColumns();
            IsLoading = false;
        }
        public void CheckAllTime(bool IsChecked, int CaHoc_ID)
        {
            foreach (DataRow r in TableTime.Rows)
            {
                if (CaHoc_ID > 0)
                {
                    if ((int)eTMS.Commons.Common.ConvertNumber(r["CaHoc_ID"]) != CaHoc_ID)
                    {
                        continue;
                    }
                }
                foreach (ThuTrongTuanEntity thuTrongTuanEntity in ListThuTrongTuan)
                {
                    r["col" + thuTrongTuanEntity.ThuTrongTuan_ID.ToString()] = IsChecked;
                }
            }
        }
        public void CheckTime(List<KeyValuePair<int,int>>ListTime)
        {
            foreach (KeyValuePair<int,int>thuTiet in ListTime)
            {
                DataRow[] rs = TableTime.Select("TietHoc_ID="+thuTiet.Value.ToString());
                foreach (DataRow r in rs)
                {
                    r["col" + thuTiet.Key.ToString()] = true;
                }
            }
        }
        public void SetTimeSlot(bool[,] TimeSlots)
        {
            for (int i = 0; i <= TimeSlots.GetUpperBound(0); i++)
            {
                for (int j = 0; j <= TimeSlots.GetUpperBound(1); j++)
                {
                    TableTime.Rows[i][j + STARTCOL] = TimeSlots[i, j];
                }
            }
        }
        public bool[,] GetTimeSlot()
        {
            bool[,] TimeSlots = new bool[TableTime.Rows.Count, ListThuTrongTuan.Count];
            for (int i = 0; i <= TimeSlots.GetUpperBound(0); i++)
            {
                for (int j = 0; j <= TimeSlots.GetUpperBound(1); j++)
                {
                    TimeSlots[i, j] = Convert.ToBoolean(TableTime.Rows[i][j + STARTCOL]);
                }
            }
            return TimeSlots;
        }
        private bool IsInternal = false;
        public event DataGridViewCellEventHandler OnCellChanged;
        private void gridThoiGian_CellValueChanged(object sender, DataGridViewCellEventArgs e)
        {
            if(IsLoading)return;
           
            if (OnCellChanged != null)
            {
                OnCellChanged(sender, e);
            }
        }
        public bool GetValue(int rowIndex, int colIndex)
        {
            return Convert.ToBoolean(TableTime.Rows[rowIndex][colIndex]);
        }

        private void gridThoiGian_CellValidating(object sender, DataGridViewCellValidatingEventArgs e)
        {
            if (!IsInternal)
            {
                if (e.ColumnIndex >= STARTCOL)
                {
                    IsInternal = true;
                    foreach (DataGridViewCell cell in gridThoiGian.SelectedCells)
                    {
                        if (cell.ColumnIndex >= STARTCOL)
                        {
                            if (cell.RowIndex >= 0)
                            {
                                if ((e.RowIndex != cell.RowIndex) || (e.ColumnIndex != cell.ColumnIndex))
                                {
                                    cell.Value = e.FormattedValue;
                                }
                            }
                        }
                    }
                }
                IsInternal = false;
            }
        }
    }
}
